package com.time.hellotime.servermodel;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.annotations.SerializedName;
import com.yanzhenjie.nohttp.Logger;

/**
 * <p>
 * 封装了业务逻辑的JavaBean
 * </p>
 * Created by Bing on 2016/8/23.
 * E-mail is tx16bing@163.com
 */
public class BasicBean {

    @SerializedName("statusCode")
    private String statusCode;

    @SerializedName("msg")
    private String msg;

    @SerializedName("data")
    private JsonObject data;

    public BasicBean() {
        // Nothing to do
    }

    public BasicBean(String statusCode, String msg, JsonObject data) {
        this.statusCode = statusCode;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 你的{@link T}必须提供默认无参构造
     *
     * @param clazz
     * @return
     */
    public <T> T parseData(Class<T> clazz) {
        T t = null;
        try {
            Logger.i(getData().toString());
            t = new Gson().fromJson(getData().toString(), clazz);
        } catch (Exception e1) {
            e1.printStackTrace();
            try {
                // 传进来的JavaBean一定要提供无参构造器
                t = clazz.newInstance();
            } catch (Exception e2) {

            }
        }
        return t;
    }

    public String getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(String statusCode) {
        this.statusCode = statusCode;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public JsonObject getData() {
        return data;
    }

    public void setData(JsonObject data) {
        this.data = data;
    }

    public boolean isSuccess() {
        return TextUtils.equals(getStatusCode(), "10001");
    }

    public boolean isNothing() {
        return TextUtils.equals(getStatusCode(), "10005");
    }

    public boolean isChangeCodeType() {
        return TextUtils.equals(getStatusCode(), "10021");
    }

    public boolean overDistance(){
        return TextUtils.equals(getStatusCode(), "10035");
    }

    @Override
    public String toString() {
        return "BasicBean{" +
                "statusCode='" + statusCode + '\'' +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
